// pages/InvitationCode/InvitationCode.js
import Card from '../../palette/card';
Page({

  /**
   * 页面的初始数据
   */
  imagePath: '',
  history: [],
  future: [],
  isSave: false,
  /**
   * 页面的初始数据
   */
  data: {
    share_backg: "",
    nickname: "",
    imgurl: "",
    template: {},
    customActionStyle: {
      border: {
        borderColor: '#1A7AF8',
      },
      scale: {
        textIcon: '/palette/switch.png',
        imageIcon: '/palette/scale.png'
      },
      imageshow: false,
      delete: {
        icon: '/palette/close.png'
      },
    Qrcode:""
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   wx.showLoading({
      title: "图片生成中"
    });
    this.getQrcode();
    // this.setData({
    //   share_backg: siteinfo.root + "/attachment/img/index_bg.png",
    // })
  },
  getQrcode: function (e) {
    var that = this;
    that.setData({
      Qrcode:"image/coded.jpg",
      template: new Card().Invita_palette("/image/coded.jpg","/image/backg.jpg"),
    })
    setTimeout(function () {
     wx.hideLoading();
    }, 2000)
},
scan_bt: function () {
  wx.scanCode({
    success(res) {
      console.log(res)
      wx.navigateTo({
        url: "/" + res.path,
      })
    }
  });
},
promisify: api => {
  return (options, ...params) => {
    return new Promise((resolve, reject) => {
      const extras = {
        success: resolve,
        fail: reject
      }
      api({
        ...options,
        ...extras
      }, ...params)
    })
  }
},
// previewImage: function (e) {
//   var current = e.target.dataset.src;
//   var imgalist = [current]
//   wx.previewImage({
//     current: current, // 当前显示图片的http链接 
//     urls: imgalist // 需要预览的图片http链接列表 
//   })
// },
imageBt: function () {
  this.setData({
    imageshow: false
  })
},
onImgOK(e) {
  getApp().core.showLoading({
    title: "图片生成中"
  });
  this.imagePath = e.detail.path;
  this.setData({
    image: this.imagePath,
  })
  console.log(e)
  var index = e.currentTarget.dataset.index;
  if (index == 0) {
    this.setData({
      imageshow: true,
      index: index
    })
    this.saveImage(this.imagePath, index);
  } else if (index == 1) {
    this.setData({
      index: index
    })
    this.saveImage(this.imagePath, index);
  } else {
    if (this.isSave) {
      this.saveImage(this.imagePath, index);
    }
  }
},

onRevert() {
  const pre = this.history.pop()
  if (!pre) {
    return
  }
  const needRefresh = pre.index && pre.index >= 0 && pre.index <= this.data.template.views.length
  if (needRefresh) {
    if (this.data.template.views[pre.index].id === pre.view.id) {
      this.data.template.views.splice(pre.index, 1)
    } else {
      this.data.template.views.splice(pre.index, 0, pre.view)
    }
    this.future.push(pre)
  } else {
    for (let i in this.data.template.views) {
      if (this.data.template.views[i].id === pre.view.id) {
        this.future.push({
          view: this.data.template.views[i]
        })
        this.data.template.views[i] = pre.view
        break
      }
    }
  }
  const props = {
    paintPallette: this.data.template,
  }
  if (needRefresh) {
    props.template = this.data.template
  } else {
    props.action = pre
  }
  this.setData(props)
},

onRecover() {
  const fut = this.future.pop()
  if (!fut) {
    return
  }
  const needRefresh = fut.index && fut.index >= 0 && fut.index <= this.data.template.views.length
  if (needRefresh) {
    if (this.data.template.views[fut.index].id === fut.view.id) {
      this.data.template.views.splice(fut.index, 1)
    } else {
      this.data.template.views.splice(fut.index, 0, fut.view)
    }
    this.history.push(fut)
  } else {
    for (let i in this.data.template.views) {
      if (this.data.template.views[i].id === fut.view.id) {
        this.history.push({
          view: this.data.template.views[i]
        })
        this.data.template.views[i] = fut.view
        break
      }
    }
  }
  const props = {
    paintPallette: this.data.template,
  }
  if (needRefresh) {
    props.template = this.data.template
  } else {
    props.action = fut
  }
  this.setData(props)
},
previewImage:function(){
    var imgalist = [imagePath]
    wx.previewImage({
      current: imagePath, // 当前显示图片的http链接 
      urls: imgalist // 需要预览的图片http链接列表 
    })
},
saveImage(imagePath = '') {
  if (!this.isSave) {
    this.isSave = true;
    this.setData({
      paintPallette: this.data.template,
    });
  } else if (imagePath) {
    console.log(imagePath)
    this.setData({
      imageUrl: imagePath
    })

    this.isSave = false;
    getApp().core.hideLoading();
    if (this.data.index != 0) {
      wx.saveImageToPhotosAlbum({
        filePath: imagePath,
        success: function (data) {
          console.log(data);
          setTimeout(function () {
            getApp().core.showModal({
              title: "提示",
              content: "图片已保存到相册",
              showCancel: !1
            })
          }, 500)
        },
        fail: function (err) {
          console.log(err);
          if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
            console.log("用户一开始拒绝了，我们想再次发起授权")
            wx.openSetting({
              success: function (settingdata) {
                console.log(settingdata)
                if (settingdata.authSetting['scope.writePhotosAlbum']) {
                  console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                } else {
                  console.log('获取权限失败，给出不给权限就无法正常使用的提示')
                }
              },
              fail: function (data) {
                console.log(data);
                getApp().core.showModal({
                  title: "提示",
                  content: "您已拒绝相册授权，请在设置中从新授权",
                  showCancel: !1
                })
              }
            })
          }
        }
      });
    }else{
      var imgalist = [imagePath]
      wx.previewImage({
        current: imagePath, // 当前显示图片的http链接 
        urls: imgalist // 需要预览的图片http链接列表 
      })
    }

  }
},

touchEnd({
  detail
}) {
  let needRefresh = detail.index >= 0 && detail.index <= this.data.template.views.length
  if (needRefresh) {
    this.history.push({
      ...detail
    })
    if (this.data.template.views[detail.index].id === detail.view.id) {
      this.data.template.views.splice(detail.index, 1)
    } else {
      this.data.template.views.splice(detail.index, 0, detail.view)
    }
  } else {
    for (let view of this.data.template.views) {
      if (view.id === detail.view.id) {
        this.history.push({
          view: {
            ...detail.view,
            ...view
          }
        })
        view.css = detail.view.css
        break
      }
    }
  }
  this.future.length = 0
  const props = {
    paintPallette: this.data.template,
  }
  if (needRefresh) {
    props.template = this.data.template
  }
  this.setData(props);
},

/**
 * 生命周期函数--监听页面初次渲染完成
 */
onReady: function () {

},
cencel_share: function () {
  wx.navigateBack({
    delta: 1,
  })
},
/**
 * 生命周期函数--监听页面初次渲染完成
 */
onReady: function () {

},

/**
 * 生命周期函数--监听页面显示
 */
onShow: function () {

},

/**
 * 生命周期函数--监听页面隐藏
 */
onHide: function () {

},

/**
 * 生命周期函数--监听页面卸载
 */
onUnload: function () {

},

/**
 * 页面相关事件处理函数--监听用户下拉动作
 */
onPullDownRefresh: function () {

},

/**
 * 页面上拉触底事件的处理函数
 */
onReachBottom: function () {

},

/**
 * 用户点击右上角分享
 */
onShareAppMessage: function () {

}
})